package manager

import event.ReplenishListChangedEvent
import mvc.model.inventory.ClearGoodsType
import mvc.model.inventory.ReplenishGoodsType
import mvc.model.inventory.ScanGoodsType
import org.greenrobot.eventbus.EventBus
import org.json.JSONObject

object ReplenishManager : Runnable {

    val ScanGoodsType = ArrayList<ScanGoodsType>()
    val GoodsTypeInfo = ArrayList<Int>()
    val ReplenishList = ArrayList<ReplenishGoodsType>()
    val ClearList = ArrayList<ClearGoodsType>()

    fun getColsWithRow(row: Int): Int // 获取row行的货道有多少列
    {
        var cols = 0
        ScanGoodsType.forEach {
            if (it.row == row) {
                cols++
            }
        }
        return cols
    }

    /**
     * 获取补货清单返回
     */
    fun onReplenishListResult(json: JSONObject) {
        val dataBR = json.getJSONArray("dataBR")
        ReplenishList.clear()
        ClearList.clear()
        if (dataBR != null) {
            val length = dataBR.length()
            for (i in 0 until length) {
                val replenishGoodsType = ReplenishGoodsType.withJson(dataBR.getJSONObject(i))
                ReplenishList.add(replenishGoodsType)
            }
        }
        val dataQC = json.getJSONArray("dataQC")
        if (dataQC != null) {
            val length = dataQC.length()
            for (i in 0 until length) {
                val clearGoodsType = ClearGoodsType.withJson(dataQC.getJSONObject(i))
                ClearList.add(clearGoodsType)
            }
        }
        EventBus.getDefault().post(ReplenishListChangedEvent(true))
    }

    override fun run() {

    }
}
